package cn.maihe.elg.operation.web.business;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.vo.business.CreateExchangeCenterVO;
import cn.maihe.elg.operation.model.vo.business.UpdateExchangeCenterVO;
import cn.maihe.elg.operation.repository.entity.ExchangeCenterDO;
import cn.maihe.elg.operation.repository.entity.ExchangeCenterDOCondition;
import cn.maihe.elg.operation.repository.entity.ExchangeCenterVo;
import cn.maihe.elg.operation.service.business.ExchangeCenterService;
import cn.maihe.elg.operation.utils.IdGenerate;
import cn.maihe.framework.starter.base.sql.PageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 交易中心controller
 * @Author WJH
 * @Date 2021/03/25
 */
@Api(value = "交易中心管理", tags = "交易中心管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class ExchangeCenterController {
    @Resource
    private ExchangeCenterService exchangeCenterService;

    @ApiOperation(value = "分页查询交易中心", notes = "根据中心编码、名称、联系人、联系人电话模糊 分页查询")
    @RequestMapping(value = "/exchangeCenters", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "centerCode", value = "中心编码", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "centerName", value = "中心名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "linkman", value = "联系人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "linkmanMobile", value = "联系人电话", dataType = "String", paramType = "query")
    })
    public PageBo<ExchangeCenterDO> pageList(int pageNo, int pageSize, ExchangeCenterVo exchangeCenterVo) {
        return exchangeCenterService.selectPageBySql(pageNo, pageSize, exchangeCenterVo);
    }

    @ApiOperation(value = "获取交易中心接口", notes = "根据交易中心id获取交易中心")
    @ApiImplicitParam(name = "id", value = "交易中心id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/exchangeCenter/{id}", method = RequestMethod.GET)
    public ExchangeCenterDO getExchangeCenter(@PathVariable Long id) {
        ExchangeCenterDO exchangeCenterDO = exchangeCenterService.selectById(id);
        if (exchangeCenterDO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "交易中心id");
        }
        return exchangeCenterDO;
    }

    @ApiOperation(value = "新增交易中心接口", notes = "新增交易中心")
    @RequestMapping(value = "/exchangeCenter", method = RequestMethod.POST)
    public void createExchangeCenter(@Validated @RequestBody CreateExchangeCenterVO vo) {
        ExchangeCenterDO exchangeCenterDO = new ExchangeCenterDO();
        BeanUtils.copyProperties(vo, exchangeCenterDO);
        exchangeCenterDO.setId(IdGenerate.getId());
        if (exchangeCenterService.exists(ExchangeCenterDOCondition.builder().andCenterCodeEq(vo.getCenterCode()).build())) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "交易中心编码", vo.getCenterCode());
        }
        if (exchangeCenterService.exists(ExchangeCenterDOCondition.builder().andCenterNameEq(vo.getCenterName()).build())) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "交易中心名称", vo.getCenterName());
        }
        exchangeCenterService.insert(exchangeCenterDO);
    }

    @ApiOperation(value = "修改交易中心接口", notes = "根据交易中心id修改交易中心")
    @RequestMapping(value = "/exchangeCenter", method = RequestMethod.PUT)
    public void modifyExchangeCenter(@Validated @RequestBody UpdateExchangeCenterVO vo) {
        ExchangeCenterDO exchangeCenterDO = new ExchangeCenterDO();
        BeanUtils.copyProperties(vo, exchangeCenterDO);

        if (exchangeCenterService.exists(ExchangeCenterDOCondition.builder().andCenterNameEq(vo.getCenterName()).andIdNe(vo.getId()).build())) {
            throw new SystemRuntimeException(ErrorCode.EXISTS, "交易中心名称", vo.getCenterName());
        }
        exchangeCenterService.updateById(exchangeCenterDO);
    }

    @ApiOperation(value = "删除交易中心接口", notes = "根据交易中心id删除交易中心")
    @ApiImplicitParam(name = "id", value = "交易中心id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/exchangeCenter/{id}", method = RequestMethod.DELETE)
    public void deleteExchangeCenter(@PathVariable Long id) {
        if (exchangeCenterService.existsCenterLinkedUser(id)) {
            throw new SystemRuntimeException(ErrorCode.NONDELETE, "交易中心已存在关联用户");
        }
        exchangeCenterService.deleteById(id);
    }


}
